import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WindowAEvent extends JFrame implements ActionListener,FocusListener{
    JButton SubmitBtn = new JButton("提交");
    JButton QuitBtn = new JButton("取消");
    JTextField usernameTF = new JTextField(20);//用户名(TF为TextField)
    JTextField passwordTF = new JTextField(20);//密码
    JTextField passwordAgainTF = new JTextField(20);//确认密码
    JTextField emailTF = new JTextField(20);//邮箱
    JLabel UNtipLA = new JLabel(" ");//用户名错误提示位（LA为label）
    JLabel PWtipLA= new JLabel(" ");//密码错误提示
    JLabel PWANtipLA = new JLabel(" ");//确认密码
    JLabel EMtipLA = new JLabel(" ");//邮箱

    Box LAbox;
    Box TFbox;
    Box LATFbox;
    Box Btnbox;
    Box Allbox;
    boolean submitOK;
    public WindowAEvent()
    {
        setLayout(new java.awt.FlowLayout());
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init()
    {
        setListener();
        LAbox = Box.createVerticalBox();
        TFbox = Box.createVerticalBox();
        LATFbox = Box.createHorizontalBox();
        Allbox = Box.createVerticalBox();
        Btnbox = Box.createHorizontalBox();

        LAbox.add(new JLabel("用户名："));
        LAbox.add(new JLabel(" "));
        LAbox.add(new JLabel("密码："));
        LAbox.add(new JLabel(" "));
        LAbox.add(new JLabel("确认密码："));
        LAbox.add(new JLabel(" "));
        LAbox.add(new JLabel("邮箱："));
        LAbox.add(new JLabel(" "));

        TFbox.add(usernameTF);
        TFbox.add(UNtipLA);
        TFbox.add(passwordTF);
        TFbox.add(PWtipLA);
        TFbox.add(passwordAgainTF);
        TFbox.add(PWANtipLA);
        TFbox.add(emailTF);
        TFbox.add(EMtipLA);

        Btnbox.add(SubmitBtn);
        Btnbox.add(Box.createHorizontalStrut(10));
        Btnbox.add(QuitBtn);

        LATFbox.add(LAbox);
        LATFbox.add(TFbox);

        Allbox.add(LATFbox);
        Allbox.add(Btnbox);

        add(Allbox);

    }
    void setListener(){
        usernameTF.addActionListener(this);
        usernameTF.addFocusListener(this);
        passwordTF.addActionListener(this);
        passwordTF.addFocusListener(this);
        passwordAgainTF.addActionListener(this);
        passwordAgainTF.addFocusListener(this);
        emailTF.addActionListener(this);
        emailTF.addFocusListener(this);

        SubmitBtn.addActionListener(this);
        QuitBtn.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
//只需要注册成功即可，别的判断在下面的失焦
        if(submitOK) {
            if (e.getSource() == SubmitBtn) {
                JOptionPane.showMessageDialog(this, "注册成功", "注册提示", JOptionPane.WARNING_MESSAGE);
            }
        }
        else if (e.getSource() == QuitBtn) {
                dispose();
            }
    }

    public void focusGained(FocusEvent e){}

    public void focusLost(FocusEvent e){
        String UNTFStr = usernameTF.getText();
        String PWTFStr = passwordTF.getText();
        String PWATFStr = passwordAgainTF.getText();
        String EMTFStr = emailTF.getText();

        if (UNTFStr.length()<6||UNTFStr.length()>20||UNTFStr.indexOf(" ")!=-1)
        {
            UNtipLA.setText("用户名必须是不含空字符的6-20位字符串");
        }
        else {
            UNtipLA.setText(" ");
        }
        if (PWTFStr.length()<8||PWTFStr.length()>20){
            PWtipLA.setText("密码必须是8-20位的可含标点符号或单词字符的字符串");
        }
        else if(!PWATFStr.equals(PWTFStr)){
            PWtipLA.setText(" ");
            PWANtipLA.setText("确认密码必须与密码相同");
        }
        else {
            UNtipLA.setText(" ");
            PWtipLA.setText(" ");
            PWANtipLA.setText(" ");
            Pattern EMP = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");
            Matcher MeM = EMP.matcher(EMTFStr);
            boolean EmOKorNOT = MeM.matches();
            if (EmOKorNOT)
            {
                EMtipLA.setText(" ");
                submitOK = true;
            }
            else {
                EMtipLA.setText("请输入合法的邮箱地址");
            }
        }
    }

}
